/**
 * @param {number[]} nums
 * @return {number}
 */
var semiOrderedPermutation = function (nums) {
  let count = 0,len=nums.length
  if(nums[0]!=1){
    let index = nums.indexOf(1)
    count+=index
    nums.splice(index,1)
    nums.unshift(1)
  }

  if(nums[len-1]!=len){
    let index = nums.indexOf(len)
    count += (len-index)

  }

  return count

 
};